
-- months_betwen
-- =============================================================================
CREATE or replace FUNCTION pg_catalog.months_between(d1 timestamp,d2 timestamp)
RETURNS integer AS
'SELECT (extract(year from age(d1,d2))*12 + extract(month from age(d1,d2)))::integer'
LANGUAGE sql;


-- sys_guid
-- =============================================================================
CREATE or replace FUNCTION pg_catalog.sys_guid()
RETURNS varchar
AS $$
    select upper(md5(random()::text || clock_timestamp()::text));
$$ LANGUAGE sql;


-- wm_concat
-- =============================================================================
drop AGGREGATE IF EXISTS pg_catalog.wm_concat(text) cascade;

CREATE OR REPLACE FUNCTION pg_catalog.vm_concat_state_func (results text, val text)
RETURNS text
LANGUAGE sql COST 50 IMMUTABLE
AS $$ select results || ',' ||val; $$;

CREATE OR REPLACE FUNCTION pg_catalog.vm_concat_final_func (results text)
RETURNS text
LANGUAGE sql COST 111 IMMUTABLE
AS $$ select substr(results, 2); $$;

CREATE AGGREGATE pg_catalog.wm_concat(text)
(
  sfunc = pg_catalog.vm_concat_state_func,
  stype = text,
  initcond = '',
  FINALFUNC = pg_catalog.vm_concat_final_func
);
